package controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.application.ConfigurableNavigationHandler;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

import model.Comment;
import model.Distr;
import model.Request;
import model.RequestModel;
import model.User;
import tests.PopulateData;

@ApplicationScoped
@ManagedBean(name="requestsBean")
public class RequestsBean {
	
	// Variables for view: main.xhtml
	
	private List<Request> requestsRaw = new ArrayList<Request>();
	
	private Request selectedRequest;
	
	private RequestModel requestProduction;
	
	// Variables for view: request.xhtml
	
	private Request editedRequest;
	
	private int indexSelectedRequest;
	
	private String newComment;
	
	//------------------------------------------------------------------------------------------------------------------------
	// methods for view: main.xhtml
	//------------------------------------------------------------------------------------------------------------------------
	
	public String addNewRequest() {
		this.editedRequest = new Request (this.requestProduction.getRowCount());
		return "request?faces-redirect=true";
	}
	
	public RequestsBean() {
			
		requestsRaw = PopulateData.populateRequestsData(10);
		
		requestProduction = new RequestModel(requestsRaw);
	}
	
	public int getIndexSelectedRequest() {
		return indexSelectedRequest;
	}
	
	public void setIndexSelectedRequest(int indexSelectedRequest) {
		this.indexSelectedRequest = indexSelectedRequest;
	}
	
	public Request getSelectedRequest() {
		return selectedRequest;
	}
	
	public void setSelectedRequest(Request request) {
		this.selectedRequest = request;
	}
	
	public Long getRequestsCount() {
		return (Long.valueOf(requestsRaw.size())); 
	}
	
	public RequestModel getRequestsTest() {
		return requestProduction;
	}
	
	public String getNewComment() {
		return newComment;
	}

	public void setNewComment(String newComment) {
		this.newComment = newComment;
	}
	
	public ArrayList<Comment> getComments() {
		return editedRequest.getComments();
	}
	
	public void onRowSelect() throws CloneNotSupportedException {
		ConfigurableNavigationHandler configurableNavigationHandler =
	             (ConfigurableNavigationHandler)FacesContext.
	               getCurrentInstance().getApplication().getNavigationHandler();
	       
	         configurableNavigationHandler.performNavigation("request?faces-redirect=true");
	         setIndexSelectedRequest(requestsRaw.indexOf(selectedRequest));
	         
	         editedRequest = selectedRequest.clone();
	}
	
	//------------------------------------------------------------------------------------------------------------------------
	// methods for view: request.xhtml
	//------------------------------------------------------------------------------------------------------------------------
	
	public String saveAndExit() {

		requestsRaw.remove(getIndexSelectedRequest());
		requestsRaw.add(getIndexSelectedRequest(), editedRequest);
		requestProduction = new RequestModel(requestsRaw);
		return "main?faces-redirect=true";
	}
	
	public String save() {

		requestsRaw.remove(getIndexSelectedRequest());
		requestsRaw.add(getIndexSelectedRequest(), editedRequest);
		requestProduction = new RequestModel(requestsRaw);
		return null;
	}
	
	public String exit() {
		return "main?faces-redirect=true";
	}
	
	public void addComment() {
		editedRequest.addComment(newComment);
	}
	
	
	public int getEditedRequestId() {
		return editedRequest.getId();
	}
	
	public String getEditedRequestName() {
		return editedRequest.getName();
	}
	
	public User getEditedRequestCustomer() {
		return editedRequest.getCustomer();
	}
	
	public String getEditedRequestCustomerFullname() {
		return editedRequest.getCustomerFullname();
	}
	
	public User getEditedRequestResponsible() {
		return editedRequest.getResponsible();
	}
	
	public User getEditedRequestAccountable() {
		return editedRequest.getAccountable();
	}

	public User getEditedRequestConsulted() {
		return editedRequest.getConsulted();
	}
	
	public User getEditedRequestInformed() {
		return editedRequest.getInformed();
	}
	
	public User getEditedRequestSupport() {
		return editedRequest.getSupport();
	}
	
	public Date getEditedRequestCreateDate() {
		return editedRequest.getCreateDate();
	}
	
	public Date getEditedRequestEndDate() {
		return editedRequest.getEndDate();
	}
	
	
	public Distr getEditedRequestDistributors() {
		return editedRequest.getDistributors();
	}

	public String getEditedRequestProcess() {
		return editedRequest.getProcess();
	}
	
	public String getEditedRequestStatus() {
		return editedRequest.getStatus();
	}

	public void setEditedRequestId(int id) {
		editedRequest.setId(id);
	}
	
	public void setEditedRequestName(String name) {
		editedRequest.setName(name);
	}
	
	public void setEditedRequestCustomer(User customer) {
		editedRequest.setCustomer(customer);
	}
	
	public void setEditedRequestResponsible(User responsible) {
		editedRequest.setResponsible(responsible); 
	}
	
	public void setEditedRequestAccountable(User accountable) {
		editedRequest.setAccountable(accountable);
	}

	public void setEditedRequestConsulted(User consulted) {
		editedRequest.setConsulted(consulted);
	}
	
	public void setEditedRequestInformed(User informed) {
		editedRequest.setInformed(informed);
	}
	
	public void setEditedRequestSupport(User support) {
		editedRequest.setSupport(support);
	}

	public void setEditedRequestCreateDate(Date newCreateDate) {
		editedRequest.setCreateDate(newCreateDate);		
	}

	public void setEditedRequestEndDate(Date newEndDate) {
		editedRequest.setEndDate(newEndDate);
	}

	public void setEditedRequestDistributors(Distr distributors) {
		editedRequest.setDistributors(distributors);
	}

	public void setEditedRequestProcess(String process) {
		editedRequest.setProcess(process);
	}
	
	public void setEditedRequestStatus(String status) {
		editedRequest.setStatus(status);
	}

}
